//
//  EditSystemInfoView.swift
//  GreenDeal
//
//  Created by Moennig Q on 3/1/2023.
//

import UIKit
import SnapKit
import RxSwift
import RxCocoa
//System 中的增加类型
enum SystemInfoAddType {
    case Panel
    case Inverter
    case Battery
}
class SystemInfoCombineView:  View {
    private lazy var disposeBag = DisposeBag()
    var scrollToViewClosure: ((SystemInfoAddType)->Void)?
    //MARK: - panel ，inverter， battery内容
    let panelTableView = EditPanelTableView()
    let inverterTableView = EditInverterTableView()
    let batteryCommonView = EditBatteryCommonView()
    //MARK: - panel ，inverter， battery 增加按钮
    lazy var panelAddBtn: UIButton = {
       let view = UIButton()
        view.setImage(R.image.jobdetail_edit_systeinfo_add_icon(), for: UIControl.State.normal)
        return view
    }()
    lazy var  inverterAddBtn: UIButton = {
        let view = UIButton()
         view.setImage(R.image.jobdetail_edit_systeinfo_add_icon(), for: UIControl.State.normal)
         return view
     }()
    lazy var  batteryAddBtn: UIButton = {
        let view = UIButton()
         view.setImage(R.image.jobdetail_edit_systeinfo_add_icon(), for: UIControl.State.normal)
         return view
     }()
    
    //constraint
    var panelTableViewHeightConstraint: Constraint?
    var inverterTableViewHeightConstraint: Constraint?
    
    lazy var addBtn: UIButton = {
        let view = UIButton()
        view.setImage(UIImage(named: "PVDDetail_systeminfo_add"), for: .normal)
        return view
    }()
    
    override func setUpView(){
        //panel title
        let panelTitle = ContentLabel(hexStr: "#292D3299")
        panelTitle.text = R.string.localizable.panelS()
        self.addSubview(panelTitle)
        panelTitle.snp.makeConstraints { make in
            make.top.equalTo(Constant.SectionTopMargin)
            make.left.equalTo(Constant.AllOutMargin)
            make.height.width.greaterThanOrEqualTo(0)
        }
        //
        self.addSubview(panelAddBtn)
        panelAddBtn.snp.makeConstraints { make in
            make.right.equalTo(-Constant.LeftRightMargin)
            make.height.width.equalTo(40)
            make.centerY.equalTo(panelTitle)
        }
        //panel table
        panelTableView.isScrollEnabled = false
        self.addSubview(panelTableView)
        panelTableView.snp.makeConstraints { make in
            make.top.equalTo(panelTitle.snp.bottom).offset(Constant.TopBottomMargin)
            make.left.equalTo(panelTitle)
            make.right.equalTo(-Constant.AllOutMargin)
            self.panelTableViewHeightConstraint = make.height.greaterThanOrEqualTo(0).constraint
        }
        //inverter title
        let inverterTitle = ContentLabel(hexStr: "#292D3299")
        inverterTitle.text = R.string.localizable.inverterS()
        self.addSubview(inverterTitle)
        inverterTitle.snp.makeConstraints { make in
            make.top.equalTo(panelTableView.snp.bottom).offset(Constant.TitleTextMargin)
            make.left.equalTo(panelTitle)
            make.height.width.greaterThanOrEqualTo(0)
        }
        //
        self.addSubview(inverterAddBtn)
        inverterAddBtn.snp.makeConstraints { make in
            make.right.size.equalTo(panelAddBtn)
            make.centerY.equalTo(inverterTitle)
        }
        //inverter table
        inverterTableView.isScrollEnabled = false
        self.addSubview(inverterTableView)
        inverterTableView.snp.makeConstraints { make in
            make.top.equalTo(inverterTitle.snp.bottom).offset(Constant.TopBottomMargin)
            make.left.right.equalTo(panelTableView)
            self.inverterTableViewHeightConstraint = make.height.greaterThanOrEqualTo(0).constraint
        }
        //battery title
        let batteryTitle = ContentLabel(hexStr: "#292D3299")
        batteryTitle.text = R.string.localizable.batterieS()
        self.addSubview(batteryTitle)
        batteryTitle.snp.makeConstraints { make in
            make.top.equalTo(inverterTableView.snp.bottom).offset(Constant.TitleTextMargin)
            make.left.equalTo(panelTitle)
            make.height.width.greaterThanOrEqualTo(0)
        }
        //
        self.addSubview(batteryAddBtn)
        batteryAddBtn.snp.makeConstraints { make in
            make.right.size.equalTo(panelAddBtn)
            make.centerY.equalTo(batteryTitle)
        }
        //battery table
        batteryCommonView.scrollView.isScrollEnabled = false
        self.addSubview(batteryCommonView)
        batteryCommonView.snp.makeConstraints { make in
            make.top.equalTo(batteryTitle.snp.bottom).offset(Constant.TopBottomMargin)
            make.left.right.bottom.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
        }
        //观察content size
        panelTableView.rx.observe(CGSize.self,"contentSize").subscribe {[weak self] (size) in
            guard let contentSize = size else {return}
            self?.panelTableViewHeightConstraint?.update(offset: contentSize.height)
        }.disposed(by: self.disposeBag)
        //观察content size
        inverterTableView.rx.observe(CGSize.self,"contentSize").subscribe {[weak self] (size) in
            guard let contentSize = size else {return}
            self?.inverterTableViewHeightConstraint?.update(offset: contentSize.height)
        }.disposed(by: self.disposeBag)
        //
        batteryCommonView.scrollView.rx.observe(CGSize.self,"contentSize").subscribe {[weak self] (size) in
            guard let contentSize = size else {return}
            self?.batteryCommonView.scrollViewHeightConstraint?.update(offset: contentSize.height)
        }.disposed(by: self.disposeBag)
        //订阅isBattery的变化，控制add battery按钮的显示
        batteryCommonView.isBattery.asObservable().subscribe(onNext: {[weak self] (onFlag) in
            self?.batteryAddBtn.isHidden = !onFlag
        }).disposed(by: disposeBag)
        //MARK: - 增加一行panel
        panelAddBtn.rx.tap.subscribe { [weak self] _ in
            guard let strongSelf = self else { return  }
            strongSelf.panelTableView.addOnePanel()
            strongSelf.scrollToViewClosure?(.Panel)
        }.disposed(by: self.disposeBag)
        //MARK: - 增加一行inverter
        inverterAddBtn.rx.tap.subscribe { [weak self] _ in
            guard let strongSelf = self else { return  }
            strongSelf.inverterTableView.addOneInverter()
            strongSelf.scrollToViewClosure?(.Inverter)
        }.disposed(by: self.disposeBag)
        //MARK: - 增加一行battery
        batteryAddBtn.rx.tap.subscribe { [weak self] _ in
            guard let strongSelf = self else { return  }
            strongSelf.batteryCommonView.addOneBattery()
            strongSelf.scrollToViewClosure?(.Battery)
        }.disposed(by: self.disposeBag)
    }
    //
    func loadData(isBattery: Bool, panels: [InstallationDetailSystemDetailForPanelModel],inverters: [InstallationDetailSystemDetailForInverterModel],batteries: [InstallationDetailSystemDetailForBatteryModel]){
        self.panelTableView.loadData(panels)
        self.inverterTableView.loadData(inverters)
        self.batteryCommonView.loadData(batteries, isBattery: isBattery)
    }
}
